<template>
	<view class="address-edit">
		<view class="form bg-white">
			<u-field v-model="addressObj.contact" label="收货人" placeholder="请填写收货人姓名">
			</u-field>
			<u-field v-model="addressObj.telephone" label="联系方式" placeholder="请填写手机号码">
			</u-field>
			<view  @click="showRegion = true">
				<u-field v-model="region" :disabled="true" label="所在地区" placeholder="请选择省、市、区" right-icon="arrow-right">
				</u-field>
			</view>
			<view>
				<u-field v-model="addressObj.address" type="textarea" label="详细地址" placeholder="请填写小区、街道、门牌号等信息"
					:field-style="{flex: 1, height: '200rpx'}" @input="getAddressInput" />
			</view>
		</view>
		<view class="m-t-10 m-b-10 bg-white p-20">
			<u-checkbox  @click="changeDefault" v-model="addressObj.is_default" shape="circle">
				<text class="xs">设置为默认</text>
			</u-checkbox>
		</view>
		<button class="my-btn bg-primary white br60" @tap="formSubmit">完成</button>
		<u-select v-model="showRegion" mode="mutil-column-auto" @confirm="regionChange" :list="lists"></u-select>
	</view>
</template>

<script>
	// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | gitee下载：https://gitee.com/likeshop_gitee
// | github下载：https://github.com/likeshop-github
// | 访问官网：https://www.likeshop.cn
// | 访问社区：https://home.likeshop.cn
// | 访问手册：http://doc.likeshop.cn
// | 微信公众号：likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用，未经许可不能去除前后端官方版权标识
// |  likeshop系列产品收费版本务必购买商业授权，购买去版权授权后，方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的，任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
	import {
		editAddress,
		getOneAddress,
		hasRegionCode,
		addAddress
	} from '@/api/user';
	import area from '@/utils/area'
	var QQMapWX = require('@/public/qqmap-wx-jssdk.min.js');
 
	// 实例化API核心类
	var qqmapsdk = new QQMapWX({
	    key: '3MHBZ-LG2WX-U4W4Z-ZQSIX-LZGMQ-MYBQ6' // 必填
	});
	export default {
		data() {
			return {
				latitude:'',
				type:'',
				longitude:'',
				addressObj: {
					contact: '',
					telephone: '',
					province: '',
					city: '',
					district: '',
					address: '',
					is_default: false
				},
				region: '',
				addressId: '',
				defaultRegion: ['广东省', '广州市', '番禺区'],
				defaultRegionCode: '440113',
				showRegion: false,
				lists: []
			};
		},
		onLoad: function(options) {
			this.type = this.$Route.query.type; //为了区分兑换实物收货地址和普通收货地址
			this.addressId = parseInt(options.id)
			// this.addressId = 1
			console.log('this.addressId',this.addressId)
			if (options.id) {
				uni.setNavigationBarTitle({
					title: '编辑地址'
				});
				this.getOneAddressFun();
			} else {
				uni.setNavigationBarTitle({
					title: '添加地址'
				});
				this.getWxAddressFun();
			}
			this.$nextTick(() => {
				this.lists = area
			})
		},

		onUnload: function() {
			uni.removeStorageSync('wxAddress');
		},

		methods: {
			getAddressInput(value){
				var that=this
				qqmapsdk.geocoder({
				      //获取表单传入地址
				      address: that.region+value, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
				      success: function(res) {//成功后的回调
				      	that.latitude=res.result.location.lat
				      	that.longitude=res.result.location.lng
				      	console.log('77',that.latitude)
				    }
				})
			},
			async formSubmit() {
				
				let {
					addressObj: {
						contact,
						telephone,
						province_id,
						city_id,
						district_id,
						is_default,
						address
					},
					addressId,
					region,
				} = this;
				if (!contact) return this.$toast({
					title: '请填写收货人姓名'
				});
				if (!telephone) return this.$toast({
					title: '请填写手机号码'
				});
				if (!region) return this.$toast({
					title: '请选择省、市、区'
				});
				if (!address) return this.$toast({
					title: '请填写小区、街道、门牌号等信息'
				});
					console.log(this.latitude)
				const params = {
					contact,
					telephone,
					latitude:this.latitude,
					longitude:this.longitude,
					province_id: parseInt(province_id),
					city_id: parseInt(city_id),
					district_id: parseInt(district_id),
					is_default: is_default ? 1 : 0,
					id: addressId,
					address,
					type:this.type,
				}
				// console.log('params',params)
				const {code, msg} = addressId ? await editAddress(params) : await addAddress(params)
				// console.log('msg',code)
				if (code == 1) {
					this.$toast({
						title: msg
					}, {
						tab: 3,
						url: 1
					});
				}
			},
			regionChange(region) {

				var that=this
				that.addressObj.province_id = region[0].value;
				that.addressObj.city_id = region[1].value;
				that.addressObj.district_id = region[2].value;
				that.region = region[0].label + " " + region[1].label + " " + region[2].label
				qqmapsdk.geocoder({
				      //获取表单传入地址
				      address: that.region+that.addressObj.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
				      success: function(res) {//成功后的回调
				      	that.latitude=res.result.location.lat
				      	that.longitude=res.result.location.lng
				    }
				})
			},

			getOneAddressFun() {
				var that=this
				getOneAddress(that.addressId).then(res => {
					if (res.code == 1) {
						let {
							city,
							province,
							district
						} = res.data;
						that.addressObj = res.data;
						that.region = `${province} ${city} ${district}`
						qqmapsdk.geocoder({
						      //获取表单传入地址
						      address: that.region+that.addressObj.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
						      success: function(res) {//成功后的回调
						      	that.latitude=res.result.location.lat
						      	that.longitude=res.result.location.lng
						    }
						})
					}
				});
			},

			getWxAddressFun() {
				let wxAddress = uni.getStorageSync('wxAddress');
				if (!wxAddress) return;
				wxAddress = JSON.parse(wxAddress)
				let {
					userName: contact,
					telNumber: telephone,
					provinceName: province,
					cityName: city,
					detailInfo: address
				} = wxAddress;
				let district = wxAddress.countryName || wxAddress.countyName
				hasRegionCode({
					province,
					city,
					district
				}).then(res => {
					if (res.code == 1) {
						
						if (res.data.province && res.data.city && res.data.district) {
							this.region = `${province} ${city} ${district}`;
							this.addressObj.province_id = res.data.province;
							this.addressObj.city_id = res.data.city;
							this.addressObj.district_id = res.data.district;
						}
						this.addressObj.contact = contact;
						this.addressObj.telephone = telephone
						this.addressObj.address = address
						console.log('2',this.addressObj.address)
					}
				});
			}

		}
	};
</script>
<style lang="scss">

	.address-edit {
		padding-top: 10rpx;


		.my-btn {
			margin: 30rpx 26rpx;
			text-align: center;
		}
	}

</style>
